using System.Collections;
using System.Collections.Generic;
using Jing.TurbochargedScrollList;
using UnityEngine;
using UnityEngine.UI;

public class MailPage : MonoBehaviour
{
    public Button btn;

    public GameObject mainPage;

    public ScrollRect ScrollRect;

    public GameObject itemPrefab;

    private BaseScrollList list;
    // Start is called before the first frame update
    void Start()
    {
        btn.onClick.AddListener(OnClick);
        
        var layout = new VerticalLayoutSettings();
        layout.gap = 10;
        layout.paddingTop = 50;
        list = new VerticalScrollList(ScrollRect, itemPrefab, layout);
        list.onRenderItem += OnItemRender;
        list.onRebuildContent += OnRebuildContent;
        list.onRefresh += OnListRefresh;
        list.AddRange(MailMgr.Instance.Datas);
    }

    private void OnClick()
    {
        gameObject.SetActive(false);
        mainPage.SetActive(true);
    }

    private void OnItemRender(ScrollListItem item, object data, bool isRefresh)
    {
        if (isRefresh)
        {
            var obj = (MailItemData)data;
            var listItem = item.GetComponent<ListItem>();
            var listRT = list.scrollRect.GetComponent<RectTransform>();
            var content = string.Format("Index:{0} Data:{1}", item.index, obj.id);
            listItem.Refresh(listRT.rect.width - 40, 100 + ((item.index % 15) * 20), content,obj.id);
        }

        //Debug.LogFormat("渲染Item [idx:{0}, value:{1}]", item.index, data);
    }
    
    private void OnRebuildContent()
    {
        //Debug.Log("列表高度改变");
    }
    
    private void OnListRefresh()
    {
        //Debug.Log("列表刷新");
    }
}
